<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-date-picker v-model="searchTime" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
                <el-button type="primary" @click="search">查询</el-button>
                <el-button type="primary" @click="exportExcel">导出
                    <i class="el-icon-download"></i>
                </el-button>
            </div>
            <div>
                <el-table :data="unlock" border fit>
                    <el-table-column prop="createTime" label="日期" align="center">
                    </el-table-column>
                    <!-- <el-table-column prop="un_lock" label="解锁次数" align="center">
                    </el-table-column>
                    <el-table-column prop="un_locks" :formatter="percent" label="解锁率" align="center">
                    </el-table-column> -->
                    <el-table-column prop="openPad" label="开机次数" align="center">
                    </el-table-column>
                    <!-- <el-table-column prop="openPads" label="开机率" :formatter="percent" align="center">
                    </el-table-column> -->
                    <el-table-column prop="openPadNum" label="pad数量" align="center">
                    </el-table-column>
                </el-table>
            </div>
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page="num" layout="total,  prev, pager, next, jumper" :total="totalElements">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { getUnlock, exportUnlock } from '../../api/server.js'
import { ip } from '../../utils/local.js'
export default {
    data() {
        return {
            searchTime: '',
            unlock: [],
            num: 1,
            totalElements: 10,
            ip: ip
        }
    },
    created() {
        this.unlocks(1)
    },
    methods: {
        handleCurrentChange(val) {
            this.unlocks(val)
        },
        search() {
            this.unlocks(1)
        },
        unlocks(index) {
            let time = this.searchTime
            let start, end
            if (time.length) {
                start = time[0]
                end = time[1]
            }
            getUnlock({ start, end, index })
                .then(res => {
                    this.unlock = res.result
                    this.totalElements = res.resultNum
                })

        },
        percent(row, column, cellval) {
            if (cellval == 0) return cellval
            return cellval + '%'
        },
        exportExcel() {
            let time = this.searchTime
            let start, end
            if (time.length) {
                start = time[0]
                end = time[1]
            }
            exportUnlock({ start, end })
                .then(res => {
                    let ip = this.ip
                    console.log('ip', ip)
                    window.open(ip + res.result, '_blank')
                })
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>